import axios from 'axios';
import qs from 'qs'

// 添加请求拦截器
axios.interceptors.request.use(
    function (config) {
      // 可以从localStorage, sessionStorage或者cookie中读取token
      const token = localStorage.getItem('token'); // 假设 token 保存在 localStorage 中
      if (token) {
        // 如果有 token，则将其添加到请求的 Authorization 头部
        // config.headers['token'] = `Bearer ${token}`;
        config.headers.token = token;
      }
      // 在发送请求之前做些什么
      return config;
    },
    function (error) {
      // 对请求错误做些什么
      return Promise.reject(error);
    }
  );

export const getRequest = (url,params) => {
    let qsParams = qs.stringify(params);
    return axios.get(url,qsParams);
};

export const postRequest = (url,params) => {
    let qsParams = qs.stringify(params);
    return axios.post(url,qsParams);
};

export const imgPostRequest = (urls,formData) => {
    return axios({
        headers: {
            'Content-Type': 'multipart/form-data'
        },
        method: 'POST',
        url: urls,
        data: formData
    });
}

export const postRequestWithFormData = (urls,formData) => {
    return axios({
        method: 'POST',
        url: urls,
        data: formData
    });
}

export const postRequestWithRaw = (urls,params) => {
    return axios({
        method: 'POST',
        url: urls,
        headers: {
            'Content-Type': 'application/json'
        },
        data: JSON.stringify(params),
    })
}

export const deleteRequest = (url,params) => {
    let qsParams = qs.stringify(params)
    return axios.delete(url,qsParams)
}

export const putRequest = (url,params) => {
    let qsParams = qs.stringify(params)
    return axios.put(url,qsParams   )
}

export const deleteHotel = (data) => {
    //data: FormData
    console.log(data);
    var config = {
        method: 'delete',
        url: 'http://localhost:8022/hotel/del',
        headers: {
             'Content-Type': 'application/json',
        },
        data: data
    };
    // data.append('file', fs.createReadStream(''));
    return axios(config)
}

export const patchRequestWithRaw = (urls,params) => {
    return axios({
        method: 'PATCH',
        url: urls,
        headers: {
            'Content-Type': 'application/json'
        },
        data: JSON.stringify(params),
    })
}